package com.net.framework.help.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.net.framework.help.application.BaseApplication;

/***
 * Ui辅助工具类
 *
 * @author zhangxiaobin
 */
public class UIUtils {

    /**
     * 获取上下文
     *
     * @return
     */
    public static Context getContext() {
        return BaseApplication.getApplication();
    }

    /**
     * 获取主线程
     *
     * @return
     */
    public static Thread getMainThread() {
        return BaseApplication.getMainThread();
    }

    /**
     * 获取主线程id
     *
     * @return
     */
    public static long getMainThreadId() {
        return BaseApplication.getMainThreadId();
    }

    /**
     * 获取到主线程的looper
     *
     * @return
     */
    public static Looper getMainThreadLooper() {
        return BaseApplication.getMainThreadLooper();
    }

    /**
     * dip转换px
     */
    public static int dip2px(int dip) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    /**
     * pxz转换dip
     */
    public static int px2dip(int px) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    /**
     * 获取主线程的handler
     */
    public static Handler getHandler() {
        return BaseApplication.getMainThreadHandler();
    }

    /**
     * 延时在主线程执行runnable
     */
    public static boolean postDelayed(Runnable runnable, long delayMillis) {
        return getHandler().postDelayed(runnable, delayMillis);
    }

    /**
     * 在主线程执行runnable
     */
    public static boolean post(Runnable runnable) {
        return getHandler().post(runnable);
    }

    /**
     * 从主线程looper里面移除runnable
     */
    public static void removeCallbacks(Runnable runnable) {
        getHandler().removeCallbacks(runnable);
    }

    /**
     * 获取布局
     *
     * @param resId
     * @return
     */
    public static View inflate(Context context, int resId) {
        return LayoutInflater.from(context).inflate(resId, null);
    }

    /**
     * 获取资源
     */
    public static Resources getResources() {

        return getContext().getResources();
    }

    /**
     * 获取文字
     */
    public static String getString(int resId) {
        return getResources().getString(resId);
    }

    /**
     * 获取文字数组
     */
    public static String[] getStringArray(int resId) {
        return getResources().getStringArray(resId);
    }

    /**
     * 获取dimen
     */
    public static int getDimens(int resId) {
        return getResources().getDimensionPixelSize(resId);
    }

    /**
     * 获取drawable
     */
    public static Drawable getDrawable(int resId) {
        return getResources().getDrawable(resId);
    }

    /**
     * 获取颜色
     */
    public static int getColor(int resId) {
        return getResources().getColor(resId);
    }

    /**
     * 获取字体大小
     */
    public static float getDimen(int resId) {
        return getResources().getDimension(resId);
    }

    /**
     * 获取颜色选择器
     */
    public static ColorStateList getColorStateList(int resId) {
        return getResources().getColorStateList(resId);
    }

    /**
     * 获取manifest中<meta-data>标签中的值
     *
     * @return
     */
    public static String getMetaData(String valueKey) {
        ApplicationInfo appInfo = null;
        try {
            appInfo = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return appInfo.metaData.getString(valueKey);
    }

    // 判断当前的线程是不是在主线程
    public static boolean isRunInMainThread() {
        return android.os.Process.myTid() == getMainThreadId();
    }

    public static void runInMainThread(Runnable runnable) {
        // 在主线程运行
        if (isRunInMainThread()) {
            runnable.run();
        } else {
            post(runnable);
        }
    }

    /**
     * 对toast的简易封装。线程安全，可以在非UI线程调用。
     */
    private static void showToastSafe(final String str, final int duration) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            return;
        }
        if (isRunInMainThread()) {
            showToast(str, duration);
        } else {
            post(new Runnable() {
                @Override
                public void run() {
                    showToast(str, duration);
                }
            });
        }
    }

    public static void longToast(int str) {
        showToastSafe(getString(str), Toast.LENGTH_LONG);
    }

    public static void shortToast(int str) {
        showToastSafe(getString(str), Toast.LENGTH_SHORT);
    }

    public static void longToast(String str) {
        showToastSafe(str, Toast.LENGTH_LONG);
    }

    public static void shortToast(String str) {
        showToastSafe(str, Toast.LENGTH_SHORT);
    }

    private static void showToast(String str, int duration) {
        Toast.makeText(getContext(), str, duration).show();
    }
}
